package com.hz.friends.activity;

import java.util.HashMap;

import android.content.Intent;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import cn.sharesdk.facebook.Facebook;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUtils;
import com.hisun.phone.core.voice.CCPCall;
import com.hisun.phone.core.voice.util.Log4Util;
import com.hz.friends.chat.Helper.CCPHelper;
import com.hz.friends.chat.Helper.CCPUtil;
import com.hz.friends.serverrequest.ALEngineConfig.ALAccessTokenType;
import com.hz.friends.serverrequest.ALEngineConfig.ALUserGender;
import com.hz.friends.serverrequest.ALEngineConfig.CallBack;
import com.hz.friends.serverrequest.ALUserEngine;
import com.hz.friends.utils.MLog;

public class LoginActivity extends BaseActivity implements CCPHelper.RegistCallBack{

	private EditText edt_uname; //用户名
	private EditText edt_upasswd; //密码
	private Button txvforgetpwd;
	private Button txvreturn;
	private final String TAG = this.getClass().getSimpleName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		initViews();
		  ShareSDK.initSDK(this);
	}

	@Override
	protected void init() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void initEvents() {
		// TODO Auto-generated method stub

	}
	//忘记密码
	public void onforgetpwd(View view){
		Intent intent = new Intent(this, FindBackMPasswordActivity.class);
		startActivity(intent);
	}
	//登录
	public void onlogin(View view){
//		startActivity(MaintabsActivity.class);
//		finish();
		
		String unmae = edt_uname.getText().toString().trim();
		String upasswd = edt_upasswd.getText().toString().trim();
		if(validateInput()){
			createWiatDialog("正在登录");
			ALUserEngine.defauleEngine().login(unmae, upasswd, new CallBack() {
				
				@Override
				public void done(Object object, AVException avexception) {
					cancleWaitDialog();
					if(avexception == null){
						showSysToast("登录成功");
						CCPHelper.getInstance().setCountInit(3);
						CCPHelper.getInstance().initVoipDevice();
						startActivity(MaintabsActivity.class);
                        finish();
					}else{
						showSysToast("登录失败,用户名或密码错误");
						avexception.printStackTrace();
					}
				}
			});
		}
	}
	//facebook登录
	public void onfacebook(View view){
//		showShare();
		final Platform weibo = ShareSDK.getPlatform(this, Facebook.NAME);
		weibo.setPlatformActionListener(new PlatformActionListener() {

			@Override
			public void onError(Platform arg0, int arg1, Throwable arg2) {
				arg2.printStackTrace();
				MLog.i(TAG, "===facebook authoer fail");
				showToast("授权失败");
				weibo.removeAccount();
			}

			@Override
			public void onComplete(Platform arg0, int arg1,
					HashMap<String, Object> arg2) {
				MLog.i(TAG, "===facebook onComplete");

				if (arg1 == Platform.ACTION_USER_INFOR) {//注册
					ALUserGender gender = ALUserGender.ALUserGenderOfUndefine;
					if("0".equals(arg0.getDb().getUserGender())){
						gender = ALUserGender.ALUserGenderOfFemale;
					}else if("1".equals(arg0.getDb().getUserGender())){
						gender = ALUserGender.ALUserGenderOfMale;
					} 
					
//					facebookRegister(arg0.getDb().getUserId(),ALAccessTokenType.ALAccessTokenTypeOfFacebook,"",
//							arg0.getDb().getUserIcon(),arg0.getDb().getUserGender());
			 
					 
				}
 

			}

			@Override
			public void onCancel(Platform arg0, int arg1) {
				MLog.i(TAG, "===facebook authoer cancel");

			}
		});
		weibo.showUser(null);//获取登陆用户资料
	}
	
	private void facebookRegister(String uid,ALAccessTokenType type,String nickname,
			String headViewURL,Boolean gender){
		ALUserEngine.defauleEngine().loginByAuthData(uid, type, nickname, headViewURL, gender, new CallBack() {
			
			@Override
			public void done(Object object, AVException avexception) {
 				if(avexception == null){
 					showToast("登录成功");
 				}else{
 					showToast("登录失败");
 				}
			}
		});
	}
	
	//返回注册
	public void onreturn(View view){
		startActivity(RegisterActivity.class);
		finish();
	}

	protected void initViews() {
		edt_uname = (EditText) findViewById(R.id.edtuname);
		edt_upasswd = (EditText) findViewById(R.id.edtupasswd);
		txvforgetpwd = (Button) findViewById(R.id.txvforgetpwd);
		txvreturn = (Button) findViewById(R.id.txvreturn);
		txvreturn.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG ); //下划线
		txvforgetpwd.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG ); //下划线

	}
	
	private boolean validateInput(){
		String unmae = edt_uname.getText().toString().trim();
		String upasswd = edt_upasswd.getText().toString().trim();
		
		if (TextUtils.isEmpty(unmae)) {
			showSysToast("请输入用户名");
			return false;
		}
		if (TextUtils.isEmpty(upasswd)) {
			showSysToast("请输入密码");
			return false;
		}
		return true;
	}

	private boolean validateAccount() {
		String unmae = edt_uname.getText().toString().trim();
		if (TextUtils.isEmpty(unmae))
			showSysToast("�������ʺ�");
		return !TextUtils.isEmpty(unmae);
	}

	private boolean validatePwd() {
		String upasswd = edt_upasswd.getText().toString().trim();
		if (TextUtils.isEmpty(upasswd))
			showSysToast("����������");
		return !TextUtils.isEmpty(upasswd);
	}

	public void login(View v) {
		if ((!validateAccount()) || (!validatePwd())) {
			return;
		}

		putAsyncTask(new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected void onPreExecute() {
				super.onPreExecute();
			}

			@Override
			protected Boolean doInBackground(Void... params) {
				try {
					Thread.sleep(1000);
					return true;
				} catch (InterruptedException e) {

				}
				return false;
			}

			@Override
			protected void onPostExecute(Boolean result) {
				super.onPostExecute(result);
				if (result) {
					startActivity(MaintabsActivity.class);
					finish();
				} else {

				}
			}
		});
	}

	public void register(View v) {
		startActivity(RegisterActivity.class);
	}

	@Override
	protected void setViews() {
		// TODO Auto-generated method stub
		
	}
	
	private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				
				break;

			default:
				break;
			}
		};
	};
	
    //连接云通讯服务器回调
	@Override
	public void onRegistResult(final int reason, final String msg) {

		runOnUiThread(new Runnable() {
			@Override
			public void run() {
					if (reason == CCPHelper.WHAT_ON_CONNECT) { //连接成功
					 
					} else if (reason == CCPHelper.WHAT_ON_DISCONNECT || reason == CCPHelper.WHAT_INIT_ERROR) { //连接失败或初始失败
						// do nothing ...
//						showSysToast("登录失败  "+msg);
//						showSysToast("登录失败  ");
						CCPUtil.exitCCPDemo();
					} else {
//						showSysToast("登录错误");
						CCPUtil.exitCCPDemo();
					}
			 
 
				CCPHelper.getInstance().setRegistCallback(null);
			}
		});
	
		
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		ShareSDK.stopSDK();
	}

}
